package top.csh215.cmd2.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.csh215.cmd2.common.Result;
import top.csh215.cmd2.entity.Permission;
import top.csh215.cmd2.entity.User;
import top.csh215.cmd2.service.UserService;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test")
@CrossOrigin
public class TestAdminController {

    @Resource
    private UserService userService;


    // 测试接口，只有管理员角色可以访问
    @SaCheckRole("ADMIN")
    @GetMapping("/admin")
    public Result<String> getAdminData() {



        return Result.success("管理员数据");
    }

    // 测试接口，只有用户列表权限可以访问
    @SaCheckPermission("sys:user:list")
    @GetMapping("/list")
    public Result<List<User>> getUserList() {
        List<User> users = userService.list();
        return Result.success(users);
    }

    // 获取用户菜单权限接口
    @GetMapping("/menus")
    public Result<List<Permission>> getUserMenus() {
        if (!StpUtil.isLogin()) {
            return Result.error("用户未登录");
        }

        Integer userId = StpUtil.getLoginIdAsInt();
        List<Permission> permissions = userService.getUserPermissions(userId);
        // 过滤出菜单类型的权限
        List<Permission> menus = permissions.stream()
                .filter(p -> p.getType() == 1) // 1表示菜单类型
                .collect(Collectors.toList());

        return Result.success(menus);
    }


}
